/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.felinelayer.client.clientSwing;

import br.com.felinelayer.common.Message;
import br.com.felinelayer.common.Protocol;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Home
 */
public class Grid extends ComponentSwing<Grid, JTable> implements br.com.felinelayer.client.IGrid {

    private JTable jtable;

    @Override
    public Grid setText(String text) {
//        throw new UnsupportedOperationException("Not supported yet.");
        return this;
    }

    @Override
    public String getText() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return "";
    }

    @Override
    protected void doInitialize() {
        jtable = new JTable(model());

        //JPanel panel = new JPanel();

        JScrollPane scroolPane = new JScrollPane(jtable);
        jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        //panel.add(scroolPane);
        initializeComponente(scroolPane);
    }

    private AbstractTableModel model() {
        return new AbstractTableModel() {
            @Override
            public int getRowCount() {
                try {
                    try {
                        Message message = createEvent(Protocol.GRID_GET_ROW_COUNT);
                        message = fireInquireEvent(message);
                        return message.getIntByName(Protocol.GRID_ROW_COUNT);
                    } catch (Exception ex) {
                        Logger.getLogger(Grid.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (Exception ex1) {
                    Logger.getLogger(Grid.class.getName()).log(Level.SEVERE, null, ex1);
                }
                return 0;
            }

            @Override
            public int getColumnCount() {
                try {
                    Message message = createEvent(Protocol.GRID_GET_COLUMN_COUNT);
                    message = fireInquireEvent(message);
                    return message.getIntByName(Protocol.GRID_COLUMN_COUNT);
                } catch (Exception ex) {
                    Logger.getLogger(Grid.class.getName()).log(Level.SEVERE, null, ex);
                }
                return 0;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                try {
                    Message message = createEvent(Protocol.GRID_GET_VALUE_CELL);
                    message.setValueByName(Protocol.GRID_ROW, rowIndex);
                    message.setValueByName(Protocol.GRID_COLUMN, rowIndex);
                    message = fireInquireEvent(message);
                    return message.getValueByName(Protocol.GRID_VALUE_CELL);
                } catch (Exception ex) {
                    Logger.getLogger(Grid.class.getName()).log(Level.SEVERE, null, ex);
                }
                return 0;
            }
        };
    }
}
